package org.feng.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import org.feng.client.base.DictClient;
import org.feng.entity.CustomDict;
import org.feng.service.CustomDictService;
import org.feng.util.DictUtil;
import org.feng.vo.CustomDictExcelVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 自定义数据字典管理器
 *
 * @author feng
 */
@Service
public class CustomDictManager {

    @Resource
    private DictClient<Long, CustomDict> dictClient;

    @Resource
    private CustomDictService customDictService;

    /**
     * 依据ID查字典，并转换数据为树形
     *
     * @param id id为负数，查所有数据；否则查对应ID、对应父ID
     * @return 树形字典数据
     */
    public List<Tree<Long>> listDictTree(Long id) {
        CustomDict dict = new CustomDict();
        if (id >= 0) {
            dict.setId(id);
        }
        // 查库
        List<CustomDict> customDicts = dictClient.list(dict);
        if (CollUtil.isEmpty(customDicts)) {
            return Collections.emptyList();
        }

        long minId = customDicts.stream().mapToLong(CustomDict::getParentId).min().orElse(0L);
        // 转换为树形
        return DictUtil.convertToTree(customDicts, minId, null);
    }

    /**
     * 新增字典数据
     *
     * @param dict 字典
     * @return 新增成功返回true;否则返回false
     */
    public boolean addDict(CustomDict dict) {
        return dictClient.addDict(dict);
    }

    /**
     * 查全部字典数据，并转换为表格vo实体
     *
     * @return 字典表格vo
     */
    public List<CustomDictExcelVo> customDictExcelVos() {
        List<CustomDict> customDicts = customDictService.list();
        if (CollUtil.isEmpty(customDicts)) {
            return Collections.emptyList();
        }

        return customDicts.stream()
                .map(source -> BeanUtil.copyProperties(source, CustomDictExcelVo.class))
                .collect(Collectors.toList());
    }

    /**
     * 批量导入字典数据
     */
    public boolean importBatch(List<CustomDictExcelVo> customDictExcelVos) {
        List<CustomDict> customDicts = customDictExcelVos.stream()
                .map(source -> BeanUtil.copyProperties(source, CustomDict.class))
                .collect(Collectors.toList());

        return dictClient.addDictList(customDicts);
    }

    /**
     * 通过字典ID查字典数据
     *
     * @param id 字典ID
     * @return 字典数据
     */
    public CustomDict selectDictById(Long id) {
        CustomDict customDict = new CustomDict();
        customDict.setId(id);
        return dictClient.selectDict(customDict);
    }

    /**
     * 通过字典key查字典数据
     *
     * @param dictKey 字典key
     * @return 字典数据
     */
    public CustomDict selectDictByKey(String dictKey) {
        CustomDict customDict = new CustomDict();
        customDict.setDictKey(dictKey);
        return dictClient.selectDict(customDict);
    }

    public List<CustomDict> listChildrenById(Long dictId) {
        return dictClient.listChildrenById(dictId);
    }
}
